package hotel.quartz.oversea.job.switchs;

import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.SwitchConstants;
import hotel.base.oversea.constant.quartz.UrlSwitchsEnums;
import hotel.base.oversea.utils.RestTemplateUtils;
import hotel.base.oversea.utils.Result;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.job.base.BaseJob;
import hotel.quartz.oversea.service.JobAndTriggerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.Calendar;

/**
 * @Auther: lmf
 * @Date: 2021/01/28/20:52
 * @Description: 检查携程的价格+房态是否有更新成功,没有进行更新
 */
@Slf4j
public class SwitchHotelCheckPriceAndStatusIsCheckJob implements BaseJob {
    //任务分组
    private String jobGroupName= SwitchConstants.JOB_GROUP_NAME_SWITCHS;
    //类名
    private String jobClassName=this.getClass().getName();

    @Autowired
    private JobAndTriggerService jobAndTriggerService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Calendar calendar = Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        if(hour<=2){
            log.info("=====凌晨0点-2点不效验酒店房型==================");
        }else{
            //如果酒店数据已经跑完,就要重新请求接口缓存酒店数据,相当于初始化数据
            log.info("==================进入定时检查售卖房型的酒店是否更新到最新的价格和房态的定时任务=============================");
          /*  String key= CommonConstants.SWITCH_SALE_HOTEL_CHECK_PRICE_AND_STATUS_IS_CHANGE;
            if(!redisService.exists(key)){
                setHotelUpList();
            }*/
            String host = CommonUrlConstants.SWITCHS_ONLINE_HOST;
            String hotelMethodGetUrl = host + UrlSwitchsEnums.SwitchCheckHotelRoomIsCreateJob.SWITCH_CHECK_SALE_HOTEL_PRICE_AND_STATUS_IS_CHANGE.getValue();
            Result result= RestTemplateUtils.get(hotelMethodGetUrl,null,restTemplate);
            /*//如果缓存有数据就进行推送
            String json= (String) redisService.get(key);
            List<Integer> hotelList= JacksonUtil.jsonToList(json,Integer.class);
            SendUtils.dingMsgNotLog("效验携程酒店价格是否全部更新", DateUtil.getCurrTime() +"开始处理共需处理"+hotelList.size()+"家", MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            if(!ObjectUtils.isEmpty(hotelList)&&hotelList.size()>0){
                int num=50;
                if(redisService.exists(CommonConstants.SWITCH_CHECK_HOTEL_PRICE_AND_STATUS_NUM)){
                    num= (int) redisService.get(CommonConstants.SWITCH_CHECK_HOTEL_PRICE_AND_STATUS_NUM);
                }
                int count=(int)Math.ceil((double) hotelList.size()/num);
                SendUtils.dingMsgNotLog("效验携程酒店价格是否全部更新", DateUtil.getCurrTime() +"共需要遍历"+count+"次，每次处理"+num+"个", MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
                for(int i=0;i<count;i++){
                    String host = CommonUrlConstants.SWITCHS_ONLINE_HOST;
                    String hotelMethodGetUrl = host + UrlSwitchsEnums.SwitchCheckHotelRoomIsCreateJob.SWITCH_CHECK_SALE_HOTEL_PRICE_AND_STATUS_IS_CHANGE.getValue();
                    Result result= RestTemplateUtils.get(hotelMethodGetUrl,null,restTemplate);
                    if(result.getCode()==CommonConstants.SUCCESS){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                SendUtils.dingMsgNotLog("效验携程酒店价格是否全部更新", DateUtil.getCurrTime() +"处理完毕", MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            }*/
       /* String host = CommonUrlConstants.SWITCHS_ONLINE_HOST;
        String hotelMethodGetUrl = host + UrlSwitchsEnums.SwitchCheckHotelRoomIsCreateJob.SWITCH_CHECK_SALE_HOTEL_PRICE_AND_STATUS_IS_CHANGE.getValue();
        Result result= RestTemplateUtils.get(hotelMethodGetUrl,null,restTemplate);
        log.info("==========返回结果为{}===========",result.toString());*/
        }
    }
    /***
     * @Author: lmf
     * @Date: 2021/3/23
     * 如果没有数据的话就先进行获取数据封装
     */
    public void setHotelUpList(){
        String host = CommonUrlConstants.SWITCHS_ONLINE_HOST;
        String hotelMethodGetUrl = host + UrlSwitchsEnums.SwitchCheckHotelRoomIsCreateJob.SWITCH_SET_CHECK_SALE_HOTEL_PRICE_AND_STATUS_IS_CHANGE_HOTEL_LIST.getValue();
        Result result= RestTemplateUtils.get(hotelMethodGetUrl,null,restTemplate);
        log.info("==========返回结果为{}===========",result.toString());
    }
}
